{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <h2>欢迎, {{ current_user.username }}</h2>
        
        {% if current_user.role == 'student' %}
            <div class="card mt-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4>我的综合评定</h4>
                    <a href="{{ url_for('export_evaluation') }}" class="btn btn-success">
                        <i class="bi bi-download"></i> 导出评定记录
                    </a>
                </div>
                <div class="card-body">
                    <table class="table" id="evaluationTable">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>学业成绩</th>
                                <th>德育素质</th>
                                <th>体育素质</th>
                                <th>能力素质</th>
                                <th>劳育素质</th>
                                <th>总分</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for eval in evaluations %}
                            <tr>
                                <td>{{ eval.semester }}</td>
                                <td>{{ eval.academic_score }}</td>
                                <td>{{ eval.moral_score }}</td>
                                <td>{{ eval.physical_score }}</td>
                                <td>{{ eval.ability_score }}</td>
                                <td>{{ eval.labor_score }}</td>
                                <td>{{ eval.total_score }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="card mt-4">
                <div class="card-header">
                    <h4>已通过的成绩详情</h4>
                </div>
                <div class="card-body">
                    <h5>学业成绩</h5>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>成绩</th>
                                <th>提交时间</th>
                                <th>审核时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in academic_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.academic_score }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>{{ record.update_time.strftime('%Y-%m-%d %H:%M') if record.update_time else '' }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    <h5 class="mt-4">德育素质</h5>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>基础分</th>
                                <th>加分项</th>
                                <th>减分项</th>
                                <th>总分</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in moral_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.base_score }}</td>
                                <td>
                                    {% for item in record.bonus_items %}
                                        {% if item.item == 'other' %}
                                            {{ item.other_content }}：+{{ item.score }}分<br>
                                        {% else %}
                                            {{ item.item }}：+{{ item.score }}分<br>
                                        {% endif %}
                                    {% endfor %}
                                </td>
                                <td>
                                    {% for item in record.penalty_items %}
                                        {% if item.item == 'other' %}
                                            {{ item.other_content }}：-{{ item.score }}分<br>
                                        {% else %}
                                            {{ item.item }}：-{{ item.score }}分<br>
                                        {% endif %}
                                    {% endfor %}
                                </td>
                                <td>{{ record.total_score }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    <h5 class="mt-4">体育素质</h5>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>课程类型</th>
                                <th>体测结果</th>
                                <th>成绩</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in physical_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.course_type }}</td>
                                <td>{{ record.test_result }}</td>
                                <td>{{ record.physical_score }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    <h5 class="mt-4">能力素质</h5>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>学术科技能力</th>
                                <th>社会实践能力</th>
                                <th>综合技能</th>
                                <th>总分</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in ability_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.academic_score }}</td>
                                <td>{{ record.social_score }}</td>
                                <td>{{ record.skill_score }}</td>
                                <td>{{ record.total_score }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    <h5 class="mt-4">劳育素质</h5>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>类型</th>
                                <th>成绩</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in labor_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.labor_type }}</td>
                                <td>{{ record.labor_score }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% elif current_user.role == 'teacher' %}
            <div class="card mt-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4>{{ current_user.class_name }}学生成绩概览</h4>
                    <a href="{{ url_for('export_evaluation') }}" class="btn btn-success">
                        <i class="bi bi-download"></i> 导出班级评定记录
                    </a>
                </div>
                <div class="card-body">
                    {% for student in students %}
                    <div class="mb-5">
                        <h5>{{ student.username }} - {{ student.real_name }}</h5>
                        
                        <!-- 综合评定总表 -->
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>学期</th>
                                    <th>学业成绩</th>
                                    <th>德育素质</th>
                                    <th>体育素质</th>
                                    <th>能力素质</th>
                                    <th>劳育素质</th>
                                    <th>总分</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for eval in class_evaluations if eval.student_id == student.id %}
                                <tr>
                                    <td>{{ eval.semester }}</td>
                                    <td>{{ eval.academic_score }}</td>
                                    <td>{{ eval.moral_score }}</td>
                                    <td>{{ eval.physical_score }}</td>
                                    <td>{{ eval.ability_score }}</td>
                                    <td>{{ eval.labor_score }}</td>
                                    <td>{{ eval.total_score }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>

                        <!-- 详细成绩折叠面板 -->
                        <div class="accordion" id="student{{ student.id }}Accordion">
                            <!-- 学业成绩详情 -->
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#student{{ student.id }}Academic">
                                        学业成绩详情
                                    </button>
                                </h2>
                                <div id="student{{ student.id }}Academic" class="accordion-collapse collapse">
                                    <div class="accordion-body">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th>学期</th>
                                                    <th>成绩</th>
                                                    <th>提交时间</th>
                                                    <th>审核时间</th>
                                                    <th>状态</th>
                                                    <th>附件</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for record in academic_records if record.student_id == student.id %}
                                                <tr>
                                                    <td>{{ record.semester }}</td>
                                                    <td>{{ record.academic_score }}</td>
                                                    <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                                    <td>{{ record.update_time.strftime('%Y-%m-%d %H:%M') if record.update_time else '' }}</td>
                                                    <td>
                                                        {% if record.status == 'pending' %}
                                                            <span class="badge bg-warning">待审核</span>
                                                        {% elif record.status == 'approved' %}
                                                            <span class="badge bg-success">已通过</span>
                                                        {% else %}
                                                            <span class="badge bg-danger">已驳回</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record.proof_file %}
                                                            <a href="{{ url_for('download_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                                                查看附件
                                                            </a>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <!-- 德育素质详情 -->
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#student{{ student.id }}Moral">
                                        德育素质详情
                                    </button>
                                </h2>
                                <div id="student{{ student.id }}Moral" class="accordion-collapse collapse">
                                    <div class="accordion-body">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th>学期</th>
                                                    <th>基础分</th>
                                                    <th>加分项</th>
                                                    <th>减分项</th>
                                                    <th>总分</th>
                                                    <th>状态</th>
                                                    <th>附件</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for record in moral_records if record.student_id == student.id %}
                                                <tr>
                                                    <td>{{ record.semester }}</td>
                                                    <td>{{ record.base_score }}</td>
                                                    <td>
                                                        {% for item in record.bonus_items %}
                                                            {% if item.item == 'other' %}
                                                                {{ item.other_content }}：+{{ item.score }}分<br>
                                                            {% else %}
                                                                {{ item.item }}：+{{ item.score }}分<br>
                                                            {% endif %}
                                                        {% endfor %}
                                                    </td>
                                                    <td>
                                                        {% for item in record.penalty_items %}
                                                            {% if item.item == 'other' %}
                                                                {{ item.other_content }}：-{{ item.score }}分<br>
                                                            {% else %}
                                                                {{ item.item }}：-{{ item.score }}分<br>
                                                            {% endif %}
                                                        {% endfor %}
                                                    </td>
                                                    <td>{{ record.total_score }}</td>
                                                    <td>
                                                        {% if record.status == 'pending' %}
                                                            <span class="badge bg-warning">待审核</span>
                                                        {% elif record.status == 'approved' %}
                                                            <span class="badge bg-success">已通过</span>
                                                        {% else %}
                                                            <span class="badge bg-danger">已驳回</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record.proof_file %}
                                                            <a href="{{ url_for('download_moral_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                                                查看附件
                                                            </a>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <!-- 体育素质详情 -->
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#student{{ student.id }}Physical">
                                        体育素质详情
                                    </button>
                                </h2>
                                <div id="student{{ student.id }}Physical" class="accordion-collapse collapse">
                                    <div class="accordion-body">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th>学期</th>
                                                    <th>课程类型</th>
                                                    <th>体测结果</th>
                                                    <th>成绩</th>
                                                    <th>状态</th>
                                                    <th>附件</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for record in physical_records if record.student_id == student.id %}
                                                <tr>
                                                    <td>{{ record.semester }}</td>
                                                    <td>{{ record.course_type }}</td>
                                                    <td>{{ record.test_result }}</td>
                                                    <td>{{ record.physical_score }}</td>
                                                    <td>
                                                        {% if record.status == 'pending' %}
                                                            <span class="badge bg-warning">待审核</span>
                                                        {% elif record.status == 'approved' %}
                                                            <span class="badge bg-success">已通过</span>
                                                        {% else %}
                                                            <span class="badge bg-danger">已驳回</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record.proof_file %}
                                                            <a href="{{ url_for('download_physical_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                                                查看附件
                                                            </a>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <!-- 能力素质详情 -->
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#student{{ student.id }}Ability">
                                        能力素质详情
                                    </button>
                                </h2>
                                <div id="student{{ student.id }}Ability" class="accordion-collapse collapse">
                                    <div class="accordion-body">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th>学期</th>
                                                    <th>学术科技能力</th>
                                                    <th>社会实践能力</th>
                                                    <th>综合技能</th>
                                                    <th>总分</th>
                                                    <th>状态</th>
                                                    <th>附件</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for record in ability_records if record.student_id == student.id %}
                                                <tr>
                                                    <td>{{ record.semester }}</td>
                                                    <td>{{ record.academic_score }}</td>
                                                    <td>{{ record.social_score }}</td>
                                                    <td>{{ record.skill_score }}</td>
                                                    <td>{{ record.total_score }}</td>
                                                    <td>
                                                        {% if record.status == 'pending' %}
                                                            <span class="badge bg-warning">待审核</span>
                                                        {% elif record.status == 'approved' %}
                                                            <span class="badge bg-success">已通过</span>
                                                        {% else %}
                                                            <span class="badge bg-danger">已驳回</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record.proof_file %}
                                                            <a href="{{ url_for('download_ability_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                                                查看附件
                                                            </a>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <!-- 劳育素质详情 -->
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#student{{ student.id }}Labor">
                                        劳育素质详情
                                    </button>
                                </h2>
                                <div id="student{{ student.id }}Labor" class="accordion-collapse collapse">
                                    <div class="accordion-body">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th>学期</th>
                                                    <th>类型</th>
                                                    <th>成绩</th>
                                                    <th>状态</th>
                                                    <th>附件</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for record in labor_records if record.student_id == student.id %}
                                                <tr>
                                                    <td>{{ record.semester }}</td>
                                                    <td>{{ record.labor_type }}</td>
                                                    <td>{{ record.labor_score }}</td>
                                                    <td>
                                                        {% if record.status == 'pending' %}
                                                            <span class="badge bg-warning">待审核</span>
                                                        {% elif record.status == 'approved' %}
                                                            <span class="badge bg-success">已通过</span>
                                                        {% else %}
                                                            <span class="badge bg-danger">已驳回</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record.proof_file %}
                                                            <a href="{{ url_for('download_labor_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                                                查看附件
                                                            </a>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
<script>
function approveScore(type, id) {
    if (confirm('确定要通过这条成绩记录吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/approve_${type}/${id}`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': token
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        })
        .then(data => {
            if (data.success) {
                alert('审核通过成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + (data.message || '未知错误'));
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}

function rejectScore(type, id) {
    if (confirm('确定要驳回这条成绩记录吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/reject_${type}/${id}`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': token
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        })
        .then(data => {
            if (data.success) {
                alert('驳回成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + (data.message || '未知错误'));
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}

$(document).ready(function() {
    $('#evaluationTable').DataTable({
        "language": {
            "url": "//cdn.datatables.net/plug-ins/1.11.5/i18n/zh.json"
        },
        "order": [[6, "desc"]], // 默认按总分降序排序
        "pageLength": 10,
        "searching": true,
        "info": true,
        "lengthChange": true
    });
});
</script>
{% endblock %} 